<template>
  <div class="user-wrapper">
    <div class="content-box">
      <notice-icon class="action"/>
      <a-dropdown>
        <span class="action ant-dropdown-link user-dropdown-menu">
          <template v-if="avatar">
            <a-avatar class="avatar" size="small" :src="avatar"/>
          </template>
          <template v-else>
            <template v-if="userInfo && userInfo.gender===1">
              <a-avatar class="avatar" size="small" src="/static/avatar/avatar1.jpg"/>
            </template>
            <template v-else>
              <a-avatar class="avatar" size="small" src="/static/avatar/avatar2.jpg"/>
            </template>
          </template>
          <span>{{ nickname }}</span>
        </span>
        <a-menu slot="overlay" class="user-dropdown-menu-wrapper">
          <a-menu-item key="1">
            <a href="javascript:;" @click="pwdModify">
              <a-icon type="setting"/>
              <span>修改密码</span>
            </a>
          </a-menu-item>
          <a-menu-item key="3">
            <a href="javascript:;" @click="handleLogout">
              <a-icon type="logout"/>
              <span>退出登录</span>
            </a>
          </a-menu-item>
        </a-menu>
      </a-dropdown>
    </div>
    <PasswordModifyModal ref="pwdModal"/>
  </div>
</template>

<script>
import NoticeIcon from '@/components/NoticeIcon'
import { mapActions, mapGetters } from 'vuex'
import PasswordModifyModal from './PasswordModifyModal'

export default {
  name: 'UserMenu',
  components: {
    PasswordModifyModal,
    NoticeIcon
  },
  computed: {
    ...mapGetters(['nickname', 'avatar', 'userInfo'])

  },
  methods: {
    ...mapActions(['Logout']),
    handleLogout () {
      this.$confirm({
        title: '提示',
        content: '真的要注销登录吗 ?',
        onOk: () => {
          return this.Logout({}).then(() => {
            setTimeout(() => {
              window.location.reload()
            }, 16)
          }).catch(err => {
            this.$message.error({
              title: '错误',
              description: err.message
            })
          })
        },
        onCancel () {
        }
      })
    },
    pwdModify () {
      this.$refs.pwdModal.showModal()
    }
  }
}
</script>
